package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 查询今日运营数据
     * @return
     */
    public BusinessDataVO businessData(LocalDateTime begin, LocalDateTime end) {
        Map<String, Object> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);
        // 今日订单数
        Integer orderCount = orderMapper.countByMap(map);
        map.put("status", Orders.COMPLETED);
        // 有效订单数
        Integer validOrderCount = orderMapper.countByMap(map);

        // 订单完成率
        Double orderCompletionRate = 0.0;
        if (orderCount != 0) {
            orderCompletionRate = validOrderCount.doubleValue() / orderCount;
        }

        // 今日营业额
        Double turnover = orderMapper.sumByMap(map);

        // 今日平均客单价
        Double unitPrice = 0.0;
        if (validOrderCount != 0) {
            unitPrice = turnover.doubleValue() / validOrderCount;
        }

        // 今日新增用户数
        Integer newUsers = userMapper.countByMap(map);

        BusinessDataVO businessDataVO = BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();

        return businessDataVO;
    }


    /**
     * 查询套餐总览
     * @return
     */
    public SetmealOverViewVO getSetmealsOverView() {

        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(StatusConstant.ENABLE);
        List<Setmeal> list = setmealMapper.list(setmeal);

        Setmeal setmeal1 = new Setmeal();
        setmeal1.setStatus(StatusConstant.DISABLE);
        List<Setmeal> list1 = setmealMapper.list(setmeal1);

        SetmealOverViewVO setmealOverViewVO = SetmealOverViewVO.builder()
                .sold(list.size())
                .discontinued(list1.size())
                .build();

        return setmealOverViewVO;
    }


    /**
     * 查询菜品总览
     * @return
     */
    public DishOverViewVO getDishesOverView() {
        Map map = new HashMap();
        // 查询已售菜品
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(map);
        // 查询已停售菜品
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByMap(map);

        DishOverViewVO dishOverViewVO = DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();

        return dishOverViewVO;
    }


    /**
     * 查询订单总览
     * @return
     */
    public OrderOverViewVO getOrdersOverView(LocalDateTime begin, LocalDateTime end) {
        Map<String, Object> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);
        Integer allOrders = orderMapper.countByMap(map);// 所有订单

        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.countByMap(map);// 待接单

        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.countByMap(map);// 待派送

        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByMap(map);// 已完成

        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByMap(map);// 已取消


        OrderOverViewVO orderOverViewVO = OrderOverViewVO.builder()
                .allOrders(allOrders)
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .build();

        return orderOverViewVO;
    }
}
